package com.loyal.loyalerp.basedata.company.controller;


import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.loyal.loyalerp.commons.ServerResponse;
import com.loyal.loyalerp.basedata.company.mapper.GyCompanyMapper;
import com.loyal.loyalerp.basedata.company.model.GyCompany;
import com.loyal.loyalerp.basedata.company.service.CompanyService;
import com.loyal.loyalerp.basedata.company.util.MobileUtil;
import com.loyal.loyalerp.model.vo.GridVO;
import com.loyal.loyalerp.utils.ValidateUtil;
/**
 * 
 * @author liuruojing
 * @data2018年3月22日
 * @version 0.1
 * 
 * @description 公司的得增删改查 
 * 
 */
@Controller
public class CompanyController {
	final static String permission = "gy_company";
	@Autowired
	CompanyService companyService;
	@Autowired
	GyCompanyMapper companyMapper;

	@RequestMapping("/basedata/company") // 显示页面
	public String companyIndex() {
		return "basedata/company/company";
	}
	@RequestMapping("/company/searchDialog")
	public String search()
	{
		return "basedata/company/search";
	}
	
/**
 * 显示编辑和新增页面
 * @param request
 * @return  url
 */
	@RequestMapping("/company/editdialog")
	public String editCompany(HttpServletRequest request) {
		
        List<GyCompany> companys=companyMapper.companyList();//公司选项下拉框
		request.setAttribute("companys",companys);
        return "basedata/company/companyEdit";
	}
	@RequestMapping("/company/detaildialog")
	public String detailCompany(HttpServletRequest request,Integer id) {
		
        GyCompany company=companyMapper.selectByPrimaryKey(id);
		request.setAttribute("company",company);
		GyCompany parentcompany=companyMapper.selectByPrimaryKey(company.getParentid());
		if(parentcompany==null)
		{
			parentcompany=new GyCompany();
			parentcompany.setCompanyname("无");
		}
		request.setAttribute("parentcompany",parentcompany);
		return "basedata/company/companyDetail";
	}

	@RequestMapping("/company/Companylist")
	@ResponseBody
	public Object CompanyList(String _search, Integer rows, Integer page, String sord, String searchField,
			String searchString,String searchOper) throws SecurityException, IllegalArgumentException, IllegalAccessException {
		
		if (!ValidateUtil.validateQuery(permission)) {
			return ServerResponse.createByError("没有权限");
		}

		if ("false".equals(_search)) {
			PageInfo<GyCompany> pageInfo = companyService.companyList(rows, page, sord);
			try {
				return GridVO.fromPageInfo(pageInfo);
			} catch (NoSuchFieldException e) {
				return null;
			}
		}

		else
		{
			if("".equals(searchOper)||"".equals(searchString)||"".equals(searchField))
			return null;
			else
			{
				PageInfo<GyCompany> pageInfo=companyService.companySearchList(rows, page, sord,searchField,searchString,searchOper);
		        try {
		        	
					return GridVO.fromPageInfo(pageInfo);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					return null;
				}
			}
		}
		
	}

	@RequestMapping("/company/delete")
	@ResponseBody
	public Object CompanyDelete(Integer id) {
		
		if (!ValidateUtil.validateDel(permission)) {
			return ServerResponse.createByError("没有权限");
		}

		if (id == null) {
			return ServerResponse.createByError("传参不正确");
		} else {
			try {
				companyService.delete(id);
				return ServerResponse.createBySuccess();

			} catch (Exception e) {
				e.printStackTrace();
				return ServerResponse.createByError(e.getMessage());
			}

		}
	}

	@RequestMapping("/company/add")
	@ResponseBody
	public Object companyAdd(GyCompany company)
	{
		if (!ValidateUtil.validateAdd(permission)) {
			return ServerResponse.createByError("没有权限");
		}

		if(company.getTelephone()!=null&&!("".equals(company.getTelephone())))
		{
			//如果是非法手机号码
			if(!(MobileUtil.isPhone(company.getTelephone())||MobileUtil.isMobile(company.getTelephone())))
			{
				return ServerResponse.createByError("联系电话不符合规范");
			}
		}
		if("".equals(company.getCompanycode())||"".equals(company.getCompanyname())||"".equals(company.getActive()))
		{
			return ServerResponse.createByError("请填写必要参数");
		}
		
		try 
		{
		  	companyService.add(company);
		  	return ServerResponse.createBySuccess("新增成功");
		}
		catch(DuplicateKeyException e)
		{
			return ServerResponse.createByError("请确定您的公司编号和名称是否已存在");
		}
		catch (DataIntegrityViolationException e) {
			//e.printStackTrace();
			return ServerResponse.createByError("填写信息不符合规范");
		}
		
		
		
	}
	@RequestMapping("/company/edit")
	@ResponseBody
	public Object edit(GyCompany company)
	{
		if (!ValidateUtil.validateEdit(permission)) {
			return ServerResponse.createByError("没有权限");
		}
		if (ValidateUtil.validateEdit(permission)) {
			System.out.println("you quanxina");
		}

		if(company.getTelephone()!=null&&!("".equals(company.getTelephone())))
		{
			//如果是非法手机号码
			if(!(MobileUtil.isPhone(company.getTelephone())||MobileUtil.isMobile(company.getTelephone())))
			{
				return ServerResponse.createByError("联系电话不符合规范");
			}
		}
		if("".equals(company.getCompanycode())||"".equals(company.getCompanyname())||"".equals(company.getActive()))
		{
			return ServerResponse.createByError("请填写必要参数");
		}
		try 
		{
		companyService.edit(company);
		return ServerResponse.createBySuccess();
		}
		catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ServerResponse.createByError("修改失败");
		}
		
	}

	
}
